package com.lanou3g.springmvc.web;

import com.lanou3g.springmvc.bean.User;
import com.lanou3g.springmvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("/user")
@RestController // 这个注解其实是组合了下面两个注解的作用
//@Controller
//@ResponseBody
public class UserRestController {

    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @GetMapping("/{id}") // 两种方法等效
    public User getUserById(@PathVariable Integer id) {
        return userService.findById(id);
    }

    @GetMapping("/")
    public List<User> getAllUser() {
        return userService.queryUser();
    }

    @DeleteMapping("/{id}")
    public int deleteUser(@PathVariable Integer id) {
        return userService.deleteById(id);
    }

    @PutMapping("/")
    public int updateUser(User user) {
        return userService.update(user);
    }

    @PostMapping("/")
    public int saveUser(User user, HttpServletRequest req) {    //获取原生的Servlet对象
        return userService.saveUser(user);
    }

}
